<?php
/**
 * 
 * Contact Information
 * 
 */

use Fox\Html\Button\Icon;
use Hive\Mission;
use Fox\Grid\FieldCat\Field\Renderer\Anchor;
use Fox\Dao\SqlFragment;
use Fox\Generator;
use Fox\Behaviors;

$xview = Xview::instance();


$block = new Fox\Html\Block("bContacts",Msg(Hive::BUNDLE,Hive::CONTACTS));
$grid = new Fox\Grid("gContacts");
$grid->fieldcat(array("first_name","last_name","role","phone","email","idele"));

global $roleLabels;
$finder = new Hive\Mission\Partner\Role();
$roles = $finder->find(new SqlFragment("language = ?",array($xview->language())));
$roleLabels = array();
foreach($roles as $role) {
	$roleLabels[$role->getId()] = $role->label;
}
$grid->fieldcat()->setGenerators(array(
	'email' => Generator::useFunction(function($data){
		if(!empty($data['pro_email'])) 
		{
			return $data['pro_email'];
		}
		if(!empty($data['email'])) 
		{
			return $data['email'];
		}
		
	}),
	'phone' => Generator::useFunction(function($data){
		if(!empty($data['pro_phone1'])) 
		{
			return $data['pro_phone1'];
		}
		if(!empty($data['perso_phone1'])) 
		{
			return $data['perso_phone1'];
		}
	}),
	'role' => Generator::useFunction(function($data){
		global $roleLabels;
		if(isset($roleLabels[$data['role']])) {
			return $roleLabels[$data['role']];
		}
	}),
	
));

$callback = Generator::ajaxReload("MC_gContacts",
			Generator::url("missions","contacts","list",array("missionid" => Generator::useElement('missionid'))));

$grid->fieldcat()
	->field('idele')
	->setGenerator(Generator::ajax(
						Generator::url(Mission::BUNDLE_NAME,"contacts","remove",array(
							'missionid' => Generator::useElement('missionid'),
							'role' => Generator::useElement('role'),
							'partnerid' => Generator::useElement('partnerid'),
						)), '&confirmation=true', $callback));
// @TODO make field inputable
$block->add($grid);
return $block;
